using System;
using System.Collections.Generic;

[Serializable]
public class MatchAchievement : GameplayAchievement
{
	[Serializable]
	public class Stage
	{
		public int stageValue;

		public string reward;
	}

	public const AchievementResource.AchievementType achievementType = AchievementResource.AchievementType.MatchAchievement;

	public bool shouldWin;

	public int minimumScoreDifference;

	public bool careerMatchesOnly;

	public bool multiplayerMatchesOnly;

	public bool canUseAnyMultiplayerRoom;

	public List<MultiplayerRoomType> multiplayerRoomsEligible = new List<MultiplayerRoomType>();

	public List<Stage> stages = new List<Stage>();

	public override bool shouldShowAchievementStageCompleted
	{
		get
		{
			return model.stageReported < achievedStagesCount;
		}
	}

	public override int achievedStagesCount
	{
		get
		{
			for (int i = 0; i < stages.Count; i++)
			{
				if (model.scoreAchieved < stages[i].stageValue)
				{
					return i;
				}
			}
			return stages.Count;
		}
	}

	public override int totalStages
	{
		get
		{
			return stages.Count;
		}
	}

	public override void OnShowAchievementStageCompleted(int achievedStageCount)
	{
		model.stageReported = achievedStageCount;
		ApplyRewards();
		Save();
	}

	public override void UpdateAchievement(GGStoryAchivementsController.UpdateAchievementParams updateParams)
	{
		GGStoryAchivementsController.UpdateAchievementOnMatchResultParams updateAchievementOnMatchResultParams = updateParams as GGStoryAchivementsController.UpdateAchievementOnMatchResultParams;
		if (updateAchievementOnMatchResultParams != null)
		{
			ReportMatchCompleted(updateAchievementOnMatchResultParams);
			if (shouldShowAchievementStageCompleted)
			{
				achivementController.AchievementAccomplishedNotify(this);
			}
		}
	}

	public void ReportMatchCompleted(GGStoryAchivementsController.UpdateAchievementOnMatchResultParams matchResultParams)
	{
		if (shouldWin == matchResultParams.isPlayerWinner && matchResultParams.scoreDifference >= minimumScoreDifference && (!careerMatchesOnly || matchResultParams.matchParams.matchType == MatchType.SinglePlayer) && (!multiplayerMatchesOnly || matchResultParams.matchParams.matchType == MatchType.MultiPlayer) && (!multiplayerMatchesOnly || canUseAnyMultiplayerRoom || matchResultParams.matchParams.multiplayerParams.useRoomParams) && (!multiplayerMatchesOnly || canUseAnyMultiplayerRoom || !IsRoomEligible(matchResultParams.matchParams.multiplayerParams.roomType)))
		{
			model.scoreAchieved++;
			Save();
		}
	}

	private bool IsRoomEligible(MultiplayerRoomType roomType)
	{
		foreach (MultiplayerRoomType item in multiplayerRoomsEligible)
		{
			if (item == roomType)
			{
				return true;
			}
		}
		return false;
	}

	public override void ApplyRewards()
	{
	}

	public override bool HasCompletedStage(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return false;
		}
		return stageIndex < achievedStagesCount;
	}

	public override string GetStageText(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return string.Empty;
		}
		return stages[stageIndex].stageValue.ToString();
	}
}
